var SEG=";";
//Expand JS Functions
String.prototype.trim = function() {
    return this.replace(/(^\s+|\s+$)/g, "");
}

String.prototype.ltrim = function() {
    return this.replace(/^\s+/g, "");
}

String.prototype.rtrim = function() {
    return this.replace(/\s+$/g, "");
}

Date.prototype.format = function (fmt) {
    var week = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
    var s = fmt;

    s = s.replace(/H{2}/g, this.getHours() < 10 ? "0" + this.getHours() : this.getHours());
    s = s.replace(/H{1}/g, this.getHours());
    s = s.replace(/m{2}/g, this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes());
    s = s.replace(/m{1}/g, this.getMinutes());
    s = s.replace(/s{2}/g, this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds());
    s = s.replace(/s{1}/g, this.getSeconds());

    s = s.replace(/y{4}/g, this.getFullYear());
    s = s.replace(/y{2}/g, this.getYear());
    s = s.replace(/M{2}/g, (this.getMonth() + 1 < 10) ? "0" + (this.getMonth() + 1) : this.getMonth() + 1);
    s = s.replace(/M{1}/g, this.getMonth() + 1);
    s = s.replace(/w{3}/g, week[this.getDay()]);
    s = s.replace(/d{2}/g, this.getDate() < 10 ? "0" + this.getDate() : this.getDate());
    s = s.replace(/d{1}/g, this.getDate());

    return s;
}

Date.parseDate = function (dateStr) {
    var lre = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
    var sre = /^(\d{4})-(\d{2})-(\d{2})$/;
    var result;
    if (lre.test(dateStr)) {
        result = new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6);
    } else if (sre.test(dateStr)) {
        result = new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3);
    } else {
        throw new Error(9001, "Error format.");
    }
    return result;
}

Number.prototype.format = function (fmt) {
    //TODO '0.00'
    return this;
}

Array.prototype.contain = function (obj) {
    var arr = this;
    for (var i = 0; i < arr.length; i++) {
        if (obj == arr[i]) {
            return true;
        }
    }
    return false;
}


//Some Functions often used.
function createHttpRequest() {
    if (window.ActiveXObject) {
        return new ActiveXObject('Microsoft.XMLHTTP');
    } else if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else {
        throw new Error(9999, '无法创建HttpRequest！');
    }
}

function switchDisplay(objId) {
	var obj = document.getElementById(objId);
    if (obj == null) {
        return;
    }
    if (obj.style.display==""||obj.style.display == "none") {
        obj.style.display = "block";
    } else {
        obj.style.display = "none";
    }
}

function displayDetail(detailId, imgId) {
    var detail = document.getElementById(detailId);
    var img = document.getElementById(detailId + "_img");
    if (detail.style.display == "none") {
        detail.style.display = "block";
        img.src = "images/Minus.gif";
    } else {
        detail.style.display = "none";
        img.src = "images/Plus.gif";
    }
}

function setFieldFromUrl(labelField, valueField, url) {
    var rs = showModalDialog(url);
    if (rs != null) {
        labelField.value = rs;
        valueField.value = rs;
    }
}

function selectAll(vle, boxName) {
    var suitIdBoxes = document.getElementsByName(boxName);
    for (var i = 0; i < suitIdBoxes.length; i++) {
        var suitIdBox = suitIdBoxes[i];
        suitIdBox.checked = vle;
    }
}

function calcTop(obj) {
    var tmpObj = obj;
    var tmpTop = tmpObj.offsetTop;
    
    while (tmpObj == tmpObj.offsetParent) {
        tmpTop += tmpObj.offsetTop;
    }
    
    return tmpTop;
}

function calcLeft(obj) {
    var tmpObj = obj;
    var tmpLeft = tmpObj.offsetLeft;
    
    while (tmpObj == tmpObj.offsetParent) {
        tmpLeft += tmpObj.offsetLeft;
    }
    
    return tmpLeft;
}


///////////////////
//new define


function setField(labelField, valueField, dialogUrl) {
    var temp = showModalDialog(dialogUrl);

    if (temp != null) {
        var re = /<option><label>(.*)<\/label><value>(.*)<\/value><\/option>/;
        re.test(temp);
        labelField.value = RegExp.$1;
        valueField.value = RegExp.$2;
        return true;
    } else {
        return false;
    }
}


function pickUpOption(temp){
    return [l,v];
}

//删除确认
function confirmDelete(){
	if(window.confirm("删除的记录不可恢复，您确定要删除吗？")){
		return true;
	}else{
		return false;
	}
}


//验证select已经选中某项
function validSelectOption(sele,min,max){
	if(!min){
		min=1;
	}
	if(!max){
		max=1000;
	}
	if(sele.selectedIndex<min||sele.selectedIndex>max){
		alert("需选择\""+sele.attr("name")+"\"下拉列表的某一项");
		return false;
	}
	return true;
}

//根据tabIndex值，载入select的选项
function loadSelectOption(){
	var attr="tabindex";
	var sels=document.getElementsByTagName("select");
	var op;
	for(var i=0;i<sels.length;i++){
		op=sels[i];
		var s=op.tabIndex;
		if(s!=null&&s>=0){
			op.selectedIndex=s;
		}
	}
}


function returnOption(label, value) {
//    var temp = "<option><label>" + label + "</label><value>" + value + "</value></option>";
	var temp=[label,value];
    window.returnValue = temp;
    window.close();
}

//翻页page
function showTurnPage(pc,func) {
	var currentPage=document.getElementById("currentPage");
	currentPage.value=pc;
	var form=document.forms[0];
	if(func&&func.length>0){
		eval(func+"();");
	}
	form.submit();
}

//清理页面所有输入框
function clearInput(){
	var ins=document.getElementsByTagName("input");
	var inp;
	for(var i=0;i<ins.length;i++){
		inp=ins[i];
		if(inp.type&&inp.type=="text"){
			inp.value="";
		}
	}
	
	var sels=document.getElementsByTagName("select");
	var op;
	for(var i=0;i<sels.length;i++){
		op=sels[i];
		op.selectedIndex=0;
	}
}

//截断浮点数小数位数，默认小数点后3位
function roundFloat(fl,pos){
	if(null==pos){
		pos=3;
	}
	pos=Math.pow(10,pos);
	return Math.round(fl*pos)/pos;
}


/***************************/
//打开模式窗口
function openModalDialog(url,width,height,args,pp){
	var prop;
	if(width==null||height==null){
		prop="dialogWidth:"+650+"px;dialogHeight:"+550+"px;";
	}else{
		prop="dialogWidth:"+width+"px;dialogHeight:"+height+"px;";
	}
	prop="center:1;help:0;resizable:0;scroll:0;status:0;"+prop;
	if(pp!=null){
		prop=prop+pp;
	}
	if(!args){
		args="";
	}
	return window.showModalDialog(url,args,prop);
}

//打开通用查询新窗口
function openBackupCodeSearchWin(action,param){
	var dest=document.getElementById("backupCode");
	var url=action+"?"+param;
	var revl=openModalDialog(url);
	if(revl!=null){
		dest.value=revl;
	}
}

//返回打开新窗口后用户选择的值
function windowReturnValue(val){
	window.returnValue=val;
	window.opener=null;
	window.close();
}

//打开通用Code2Name查询新窗口
function openCommonCode2NameSearchWin2(id,type,action){
	var hidden=document.getElementById(id+"1");//input code
	var dest=document.getElementById(id+"2");//show name
	var param="";
	
	if(hidden.value!=null&&hidden.value.length!=0){
		param="code="+hidden.value;
	}else{
		param="code=";
	}
	
	if(type){
		param=param+"&type="+type;
	}else{
		param=param+"&type="+id;
	}
	if(!action){
		action="CommonSearchCode2Name.do"; //default
	}
	var url=action+"?"+param;
	var revl=openModalDialog(url,650,520);
	if(revl!=null&&revl.length==2){
		hidden.value=revl[0];//code;
		dest.value=revl[1];//name
	}
}

function openCommonCode2NameSearchWin(id,type,multiOption,action){
	var hidden=document.getElementById(id+"1");//input codes
	var dest=document.getElementById(id+"2");//show names
	var param="elem="+id;
	if(hidden.value!=null&&hidden.value.length!=0){
		param=param+"&codes="+hidden.value;
	}else{
		param=param+"&codes=";
	}
	if(type){
		param=param+"&type="+type;
	}else{
		param=param+"&type="+id;
	}
	if(multiOption){
		param=param+"&remark=true"
	}else{
		param=param+"&remark=false"
	}
	if(!action){
		action="CommonSearchCode2Name.do"; //default
	}
	var url=action+"?"+param;
	var revl=openModalDialog(url,650,520,document);
	if(revl!=null&&revl.length==2){
		hidden.value=revl[0]//code;
		dest.value=revl[1];//name
	}
}

function clearCommonCode2NameSearchInput(id){
	var hidden=document.getElementById(id+"1");
	var dest=document.getElementById(id+"2");
	hidden.value="";
	dest.value="";
}

function pitchUpOption(code,name,elem){
	var src=window.event.srcElement;
	var codes=document.getElementById("codes");
	var hidden=window.dialogArguments.getElementById(elem+"1");
	var show=window.dialogArguments.getElementById(elem+"2");
	if(src.checked){
		_addValueNoRepeat(show,name);
		_addValueNoRepeat(hidden,code);
//		show.value=show.value+name+SEG;
//		hidden.value=hidden.value+code+SEG;
	}else{
		show.value=show.value.replace(name+SEG,"");
		hidden.value=hidden.value.replace(code+SEG,"");
	}
	//codes.value=hidden.value;
	//alert(hidden.value+"=="+show.value+"=="+codes.value);
}

function _addValueNoRepeat(ele,val){
	if(!ele.value||ele.value.length==0){
		ele.value=val+SEG;
		return;
	}
	if(ele.value.lastIndexOf(SEG)!=(ele.value.length-1)){
		ele.value=ele.value+SEG;
	}
	if(ele.value.indexOf(val+SEG)!=-1){
		return;
	}else{
		ele.value=ele.value+val+SEG;
	}
}

//返回Code2Name后用户选择的值 //单值
function commonCode2NameReturnValue(code,name){
	var arr=new Array();
	arr[0]=code;
	arr[1]=name;
	window.returnValue=arr;
	window.opener=null;
	window.close();
}

//返回Code2Name单值
function commonCode2NameReturnValue(code,name){
	var arr=new Array();
	arr[0]=code;
	arr[1]=name;
	window.returnValue=arr;
	window.opener=null;
	window.close();
}

/***************************/
//在登录页面判断是否在frame中
function outFrameOnSessionInValidation(){
	var pw=window.top;
	if(pw!=null&&pw!=window){
		pw.alert("用户会话失效，需重新登录.");
		pw.location.reload(true);
	}
}